﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Serialization;
using Code;
using Desktop.Code;
 
using Desktop.SHCMPService;

namespace Desktop
{
    /// <summary>
    /// SecondWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SecondWindow 
    {
        SHCMPService.SHCMPServiceSoapClient webseService = new SHCMPServiceSoapClient();
        public SecondWindow()
        {
            InitializeComponent();

            string businessxml = webseService.GetBusiness(Convert.ToString(Application.Current.Properties["PersonGuid"]));


            BussinessDataGrid.ItemsSource = DeserializeDataTable(businessxml.Trim()).DefaultView;

            BussinessDataGrid.IsReadOnly = true;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
 
            var selectedItem = BussinessDataGrid.SelectedItem;
            int selectcoubt = BussinessDataGrid.SelectedItems.Count;
            if (selectcoubt <= 0)
            {
                MessageBox.Show("请选择业务后操作。");
                return;
            }
            var b = selectedItem as DataRowView;

            
            string userguid = Convert.ToString(Application.Current.Properties["PersonGuid"]);
            string ordercount = webseService.GetOrderCount(userguid);
            if (Convert.ToInt32(ordercount) > 0)
            {
                MessageBox.Show("您还有指令没有执行完，请稍后操作。");
            }
            else
            {
                string isinsert=webseService.InsertOrder(userguid, Convert.ToString(b.Row[0]), "2");
                if (isinsert=="1")
                {
                    MessageBox.Show("操作完成，请等候评价。");
                }
                else
                {
                    MessageBox.Show("未知错误，请联系管理员");
                }
            }
            
            
            
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            var selectedItem = this.BussinessDataGrid.SelectedItem;
            int selectcoubt = BussinessDataGrid.SelectedItems.Count;
            if (selectcoubt <= 0)
            {
                MessageBox.Show("请选择业务后操作。");
                return;
            }

            var b = selectedItem as DataRowView;


            string userguid = Convert.ToString(Application.Current.Properties["PersonGuid"]);
            string ordercount = webseService.GetOrderCount(userguid);
            if (Convert.ToInt32(ordercount) > 0)
            {
                MessageBox.Show("您还有指令没有执行完，请稍后操作。");
            }
            else
            {
                string isinsert = webseService.InsertOrder(userguid, Convert.ToString(b.Row[0]), "1");
                 if (isinsert=="1")
                {
                    MessageBox.Show("操作完成。");
                }
                else
                {
                    MessageBox.Show("未知错误，请联系管理员");
                }
            }


        }

        public DataTable DeserializeDataTable(string pXml)
        {

            StringReader strReader = new StringReader(pXml);
            XmlReader xmlReader = XmlReader.Create(strReader);
            XmlSerializer serializer = new XmlSerializer(typeof (DataTable));

            DataTable dt = serializer.Deserialize(xmlReader) as DataTable;

            return dt;
        }

    }
}
